<?php

namespace App\Http\Controllers\Project;

use Illuminate\Http\Request;
use App\Model\Common\Project;
use App\Model\Common\CategoryApi as Api;
use App\Model\Common\ProjectCategory as Category;

class IndexController extends CommonController
{
    public function index(Request $request)
    {
        echo json_encode($request->route());
        $site = [];

        $site['title'] = 'asdfasdf';
        $site['keywords'] = 'asdfasdf';
        $site['description'] = 'asdfadsfasdf';
        $this->assign('siteinfo', $site);

        return $this->fetch();
    }

    public function project(Request $request)
    {
        $route = $request->route();
        $parameters = $route->parameters;
        $projectid = $parameters['project'];
        $project = $this->assignProjectMenu($projectid, ['need' => ['categorys']]);

        $site = [];
        $site['title'] = $project['title'].'-'.config('app.company');
        $site['keywords'] = $project['keywords'];
        $site['description'] = $project['description'];
        $this->assign('siteinfo', $site);
        $this->assign('article', 'intro');

        return $this->fetch();
    }

    public function api(Request $request)
    {
        $route = $request->route();
        $parameters = $route->parameters;
        $projectid = $parameters['project'];
        $project = $this->assignProjectMenu($projectid, ['need' => ['categorys']]);
        $categoryid = $parameters['category'];
        $apiid = $parameters['api'];
        if (!$categoryid || !is_numeric($categoryid) || !$apiid || !is_numeric($apiid)) {
            return abort(404);
        }

        $Api = new Api();
        $api = $Api->where('status', 1)->where('category_id', $categoryid)->where('id', $apiid)->first();
        if (!$api)
        {
            return abort(404);
        }
        $api = $Api->oneDataFormat($api, ['module' => 'project']);
        $this->assign('api', $api);

        $site = [];
        $site['title'] = $api['title'].'-'.config('app.company');
        $site['keywords'] = $project['keywords'];
        $site['description'] = $project['description'];
        $this->assign('siteinfo', $site);
        $this->assign('apiid', $apiid);

        return $this->fetch();
    }

    public function common(Request $request) {
        $route = $request->route();
        $parameters = $route->parameters;
        $projectid = $parameters['project'];
        $project = $this->assignProjectMenu($projectid, ['need' => ['common_params', 'categorys', 'error_codes']]);

        $site = [];
        $site['title'] = $project['title'].'-'.config('app.company');
        $site['keywords'] = $project['keywords'];
        $site['description'] = $project['description'];
        $this->assign('siteinfo', $site);
        $this->assign('article', 'common');

        return $this->fetch();
    }

    public function history(Request $request)
    {
        $route = $request->route();
        $parameters = $route->parameters;
        $projectid = $parameters['project'];
        $project = $this->assignProjectMenu($projectid, ['need' => ['historys', 'categorys']]);

        $site = [];
        $site['title'] = $project['title'].'-'.config('app.company');
        $site['keywords'] = $project['keywords'];
        $site['description'] = $project['description'];
        $this->assign('siteinfo', $site);
        $this->assign('article', 'history');

        return $this->fetch();
    }

    public function overview(Request $request)
    {
        $route = $request->route();
        $parameters = $route->parameters;
        $projectid = $parameters['project'];
        $project = $this->assignProjectMenu($projectid, ['need' => ['categorys']]);

        $site = [];
        $site['title'] = $project['title'].'-'.config('app.company');
        $site['keywords'] = $project['keywords'];
        $site['description'] = $project['description'];
        $this->assign('siteinfo', $site);
        $this->assign('article', 'overview');

        return $this->fetch();
    }

    private function assignProjectMenu($projectid, $opt = [])
    {
        if (!$projectid || !is_numeric($projectid)) {
            return abort(404);
        }
        $Project = new Project();

        $project = $Project->getOne($projectid, $opt);
        if (!$project) {
            return abort(404);
        }
        $opt =  array_merge($opt, ['module' => 'project']);
        $project = $Project->oneDataFormat($project, $opt);
        $this->assign('project', $project);

        return $project;
    }
}
